<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleleaf.org">
<head th:insert="base">
    <meta charset="UTF-8">
    <title></title>
</head>
<script type="text/javascript" src="/js/echarts.js"></script>
<body>
<div class="layui-fluid">
    <div class="layui-row">
            <button class="layui-btn" id="import"><i class="layui-icon layui-icon-add-1"></i>导入道岔电压数据</button>
            <div style="width: 400px;height:300px; border: solid 1px black;" id="voltage">

            </div>
        <form class="layui-form" action="" method="post">
            <div class="layui-form-item">
                <label for="switchCode" class="layui-form-label">
                    道岔编号
                </label>
                <div class="layui-input-inline">
                    <select id="switchCode" name="switchCode" class="valid" lay-verify="required">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>

            <div class="layui-form-item">
                <label for="repairLevel" class="layui-form-label">
                    检修级别
                </label>
                <div class="layui-input-inline">
                    <select id="repairLevel" name="repairLevel" class="valid" lay-verify="required">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="repairPlace" class="layui-form-label">
                    检修部位
                </label>
                <div class="layui-input-inline">
                    <select id="repairPlace" name="repairPlace" class="valid" lay-verify="required">
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label for="closeTime" class="layui-form-label">截止时间</label>
                <div class="layui-input-inline">
                    <input class="layui-input" readonly  autocomplete="off" placeholder="截止时间" id="closeTime">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label for="finishTime" class="layui-form-label">完成日期</label>
                <div class="layui-input-inline">
                    <input class="layui-input" readonly  autocomplete="off" placeholder="完成日期" id="finishTime">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label for="remark" class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" id="remark" name="remark" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                </label>
                <button  class="layui-btn" lay-filter="add" lay-submit="">
                    确认新增
                </button>
            </div>
        </form>
    </div>
</div>
<script th:inline="javaScript">
    layui.use(['form','jquery','validate','tool','businessUtil','enums','laydate','upload'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var validate = layui.validate;
        var tool = layui.tool;
        var baseUrl = "/back/repairTask";
        var enums = layui.enums;
        var upload = layui.upload;
        var businessUtil = layui.businessUtil;
        var laydate = layui.laydate;

        init_form();

        //展示折线图
        var myChart = echarts.init(document.getElementById('voltage'));

        /**
         * 初始化页面
         */
        function init_form() {
            laydate.render({
                elem: '#closeTime'
                ,type: 'datetime'
            });
            laydate.render({
                elem: '#finishTime'
                ,type: 'datetime'
            });
            enums.createSelectByEnum('repairPlace','EnumDeviceType',['3','4'],null,'请选择检修部位');
            enums.createSelectByEnum('repairLevel','EnumRepairLevel',null,null,'请选择检修级别');
            businessUtil.createSwitchInfo('switchCode','-1',null,'请选择道岔编号');
            form.render();//重新渲染
        }
        //添加表单校验
        form.verify(validate);

        //监听提交
        form.on('submit(add)', function(data){
            data.field.closeTimeStr = $("#closeTime").val().trim();
            data.field.finishTimeStr = $("#finishTime").val().trim();
            var res = tool.submit(baseUrl + '/add',data.field);
            if(res.code == return_200){
                layer.msg(res.msg, {
                        icon: msg.success,
                        time: 2000
                    },
                    function(){
                        xadmin.father_reload();
                    }
                );
            }else {
                layer.msg(res.msg, {
                        icon: msg.error,
                        time: 2000
                    },
                    function(){
                        xadmin.father_reload();
                    }
                );
            }
            return false;
        });

        /**
         * 导入电压数据
         */
        upload.render({
            elem: '#import'
            ,url: '/back/repairTask/import'
            ,accept: 'file'
            ,exts: 'mat'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    console.log(file);
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code != return_200){
                    return layer.msg(res.msg,{
                        icon:msg.error,
                        time: 2000
                    });
                }
                var datay =res.doublesData;
                var datax = res.intData;

                myChart.setOption({
                    title: {
                        text: '道岔电压折线图'
                    },
                    xAxis: {
                        data:datax
                    },
                    yAxis: {},
                    series : [
                        {
                            name: '访问来源',
                            type: 'line',
                            data:datay
                        }
                    ]
                })
                //上传成功
                return layer.msg('电压数据上传成功',{
                    icon: msg.success,
                    time: 2000
                });
            }
            ,error: function(){
                return layer.msg('电压数据上传异常',{
                    icon: msg.error,
                    time: 2000
                });
            }
        });
    });
</script>
</body>
</html>